package com.xboard.server.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;

import com.xboard.server.dao.FxDao;

/**
 * Handles requests for the application home page.
 */
@Controller
@RequestMapping("/fx")
public class FxController {

	@Autowired
	private FxDao fxDao;


	@RequestMapping(value="/add", method = RequestMethod.GET)
	public ModelAndView add(HttpServletRequest request, ModelMap model) {
		return new ModelAndView("add");
	}	
	
	@RequestMapping(value="/add/{symbol}/{signalValue}/{nowValue}/{profit}/{trend}", method = RequestMethod.GET)
	public ModelAndView add(@PathVariable String symbol, @PathVariable String signalValue,@PathVariable String nowValue,@PathVariable String profit,@PathVariable String trend,  
			HttpServletRequest request, ModelMap model) {
        
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("symbol", symbol);
		data.put("trend", trend);
		data.put("signalValue",signalValue);
		data.put("nowValue",nowValue);
		data.put("profit",profit);
		fxDao.addFx(data);

		return new ModelAndView("redirect:/fx/list");
	}

	@RequestMapping(value="/addTicket/{ticket}/{symbol}/{price}/{lots}/{profit}/{trend}", method = RequestMethod.GET)
	public ModelAndView addTicket(@PathVariable String ticket, @PathVariable String symbol, @PathVariable String price,@PathVariable String lots,@PathVariable String profit,@PathVariable String trend, 
			HttpServletRequest request, ModelMap model) {
        
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("ticket", ticket);
		data.put("symbol", symbol);
		data.put("trend", trend);
		data.put("lots",lots);
		data.put("price",price);
		data.put("profit",profit);
		
		fxDao.addTicket(data);

		return new ModelAndView("redirect:/fx/history");
        
	}
	@RequestMapping(value="/addChart/{price}/{symbol}", method = RequestMethod.GET)
	public void addChart(@PathVariable String price,@PathVariable String symbol, 
			HttpServletRequest request, ModelMap model) {
        
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("price",price);
		data.put("symbol", symbol);
		
		fxDao.addChart(data);        
	}	
	@RequestMapping(value="/addCmd/{symbol}/{command}/{isopen}", method = RequestMethod.GET)
	public void addCmd(@PathVariable String symbol, @PathVariable String command,@PathVariable String isopen,
			HttpServletRequest request, ModelMap model) {
        
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("symbol", symbol);
		data.put("command", command);
		data.put("isopen", isopen);

		fxDao.addCmd(data);        
	}	
	
	@RequestMapping(value="/addAccount/{account}/{lots}/{profit}/{opencount}", method = RequestMethod.GET)
	public ModelAndView addAccount(@PathVariable String account, @PathVariable String lots,@PathVariable String profit,@PathVariable String opencount,  
			HttpServletRequest request, ModelMap model) {
        
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("account", account);
		data.put("lots", lots);
		data.put("profit",profit);
		data.put("opencount",opencount);
		fxDao.addAccount(data);

		return new ModelAndView("redirect:/fx/list");
	}	

	@RequestMapping(value="/list")
	public ModelAndView listFx() {

		List<Map<String,Object>> list= new ArrayList<Map<String,Object>>();
		list = fxDao.listFx();
		
		List<Map<String,Object>> listAccount= new ArrayList<Map<String,Object>>();
		listAccount = fxDao.listAccount();
		
		ModelAndView mav = new ModelAndView();
		mav.addObject("success",true);
		mav.addObject("rows",  list);
		mav.addObject("account",  listAccount);
	    return mav;
	}
	@RequestMapping(value="/chart/{symbol}", method = RequestMethod.GET)
	public MappingJacksonJsonView chart(@PathVariable String symbol,
			HttpServletRequest request, ModelMap model) {

		List<Map<String,Object>> list= new ArrayList<Map<String,Object>>();
		list = fxDao.chart(symbol);
		
		ArrayList data = new ArrayList();

	    for (Object o : list) {
	    	Map<String, Object> m = (Map<String, Object>)o;
	    	ArrayList al = new ArrayList();
	    	al.add(m.get("date"));
	    	al.add(Double.valueOf(m.get("price").toString()).doubleValue());
	    	data.add(al);
	    }
	    
		
	    MappingJacksonJsonView mav = new MappingJacksonJsonView();
		mav.addStaticAttribute("rows",data);
	    return mav;
	}		
	@RequestMapping(value="/history")
	public ModelAndView listHistory() {

		List<Map<String,Object>> list= new ArrayList<Map<String,Object>>();
		list = fxDao.listHistory();
		
		ModelAndView mav = new ModelAndView();
		mav.addObject("success",true);
		mav.addObject("rows",  list);
	    return mav;
	}
	@RequestMapping(value="/history/{symbol}", method = RequestMethod.GET)
	public MappingJacksonJsonView listHistory(@PathVariable String symbol,
			HttpServletRequest request, ModelMap model) {

		List<Map<String,Object>> list= new ArrayList<Map<String,Object>>();
		list = fxDao.listHistory(symbol);
		
		MappingJacksonJsonView mav = new MappingJacksonJsonView();
		mav.addStaticAttribute("success",true);
		mav.addStaticAttribute("rows",  list);
	    return mav;
	}	
	@SuppressWarnings("deprecation")
	@RequestMapping(value="/select/{symbol}", method = RequestMethod.GET)
	public ModelAndView getFx(@PathVariable String symbol,
			HttpServletRequest request, ModelMap model) {

		Map<String, Object> result = fxDao.getFx(symbol);
		
		ModelAndView mav = new ModelAndView("update");
		mav.addObject("success",true);
		mav.addObject("row",  result);
		
		return mav;
	}
	
	@RequestMapping(value="/command/{symbol}", method = RequestMethod.GET)
	public MappingJacksonJsonView getCmd(@PathVariable String symbol,
			HttpServletRequest request, ModelMap model) {

		Map<String, Object> result = fxDao.getCmd(symbol);
		
		MappingJacksonJsonView mav = new MappingJacksonJsonView();
		mav.addStaticAttribute("success",true);
		mav.addStaticAttribute("row",  result);
		
		return mav;
	}	

	@SuppressWarnings("deprecation")
	@RequestMapping(value="/update/{symbol}/{signalValue}/{nowValue}/{profit}/{trend}", method = RequestMethod.GET)
	public ModelAndView updateFx(@PathVariable String symbol,  @PathVariable String signalValue,@PathVariable String nowValue,@PathVariable String profit,@PathVariable String trend,  
			HttpServletRequest request, ModelMap model) {

		Map<String, Object> data = new HashMap<String, Object>();
		data.put("symbol", symbol);
		data.put("trend", trend);
		data.put("signalValue",signalValue);
		data.put("nowValue",nowValue);
		data.put("profit",profit);
		fxDao.updateFx(data);
		
		return new ModelAndView("redirect:/fx/list");

	}

	
	@SuppressWarnings("deprecation")
	@RequestMapping(value="/delete/{symbol}", method = RequestMethod.GET)
	public ModelAndView delete(@PathVariable String symbol,
			HttpServletRequest request, ModelMap model) {
		
		fxDao.deleteFx(symbol);
		return new ModelAndView("redirect:/fx/list");
	}
	
	@SuppressWarnings("deprecation")
	@RequestMapping(value="/delChart/{symbol}", method = RequestMethod.GET)
	public ModelAndView delChart(@PathVariable String symbol,
			HttpServletRequest request, ModelMap model) {
		
		fxDao.delChart(symbol);
		return new ModelAndView("redirect:/fx/chart/"+symbol);
	}	
	@SuppressWarnings("deprecation")
	@RequestMapping(value="/delCmd/{symbol}", method = RequestMethod.GET)
	public void delCmd(@PathVariable String symbol,
			HttpServletRequest request, ModelMap model) {
		
		fxDao.deleteCmd(symbol);
		System.out.println("delCmd/"+symbol);
	}	
}
